import { formatDate } from '../../utils/util'
import { _getQr } from '../../common/getQr'
import { _ticketList as _list, _ticketType } from '../../common/parking'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    openid: '',
    navIndex: 0,
    navs: [
      { label: '待发放', value: '待发放' },
      { label: '已发放', value: '已发放' }
    ],
    tabIndex: 0,
    tabs: [],
    list: [],
    pageIndex: 1,
    resShow: false,
    ticketIndex: 0
  },
  computed: {
    total () {
      let count = 0
      if (this.data.tabs.length > 0) {
        count = this.data.tabs[this.data.tabIndex].counts
      }
      return count
    },
    finished () {
      return this.data.list.length >= this.data.total
    },
    ticket () {
      let info = {}
      info = this.data.list[this.data.ticketIndex]
      return info
    }
  },
  methods: {
    showRes () {
      this.setData({
        resShow: true
      })
    },
    hideRes () {
      this.setData({
        resShow: false
      })
    },
    navChange(e) {
      let navIndex = parseInt(e.detail.value)
      this.setData({
        navIndex,
        tabIndex: 0
      }, () => {
        // app.loading('加载中')
        this.initDatas()
      })
    },
    check (e) {
      let index = parseInt(e.currentTarget.dataset.index)
      this.setData({
        ticketIndex: index
      }, () => {
        this.showRes()
      })
    },
    initDatas() {
      const openid = this.data.openid
      const status = this.data.navs[this.data.navIndex].value
      app.loading('加载中')
      _ticketType(openid, status).then(res => {
        if (res.data.IsSuccess) {
          let tabs = res.data.Data
          let head = tabs.pop()
          tabs.unshift(head)
          this.setData({
            tabs
          }, () => {
            this.list()
          })
        } else {
          wx.showModal({
            title: '对不起',
            content: res.data.Msg,
            showCancel: false
          })
        }
      }).catch(err => {
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    tabChange(e) {
      let tabIndex = parseInt(e.detail.value)
      this.setData({
        tabIndex
      }, () => {
        app.loading('加载中')
        this.list()
      })
    },
    list () {
      const openid = this.data.openid
      const status = this.data.navs[this.data.navIndex].value
      const pageIndex = this.data.pageIndex
      const hour = this.data.tabs[this.data.tabIndex].hours
      _list(openid, status, pageIndex, hour).then(res => {
        wx.hideLoading()
        if (res.data.IsSuccess) {
          let list = res.data.Data.map(item => {
            // item.EwmImg = _getQr(item.Code)
            let index = item.addtime.indexOf('T')
            item.addtime = item.addtime.substring(0, index)
            return item
          })
          let total = this.data.total
          this.setData({
            list
          })
        } else {
          wx.showModal({
            title: '对不起',
            content: '',
            showCancel: false
          })
        }
        wx.stopPullDownRefresh()
      }).catch(err => {
        wx.stopPullDownRefresh()
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: JSON.stringify(err) || '网络错误，请稍后再试',
          showCancel: false
        })
      })
    },
    totalQuery() {
      let openid = this.data.openid
      app.loading('加载中')
      let queryArr = []
      for (let i = 0; i < this.data.navs.length; i++) {
        queryArr.push(
          Promise.resolve(
            // _list(openid, this.data.navs[i].value, )
          )
        )
      }
      Promise.all(queryArr).then(res => {
        wx.hideLoading()
        if (res[0].data.IsSuccess) {
        } else {
          wx.showModal({
            title: '对不起',
            content: '',
            showCancel: false
          })
        }
        wx.stopPullDownRefresh()
      }).catch(err => {
        wx.stopPullDownRefresh()
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: JSON.stringify(err) || '网络错误，请稍后再试',
          showCancel: false
        })
      })
    },
    concatList() {
      const openid = this.data.openid
      const status = this.data.navs[this.data.navIndex].value
      const pageIndex = this.data.pageIndex
      const hour = this.data.tabs[this.data.tabIndex].hours
      _list(openid, status, pageIndex, hour).then(res => {
        wx.hideLoading()
        if (res.data.IsSuccess) {
          let list = res.data.Data.map(item => {
            item.EwmImg = _getQr(item.Code)
            let index = item.addtime.indexOf('T')
            item.addtime = item.addtime.substring(0, index)
            return item
          })
          this.data.list = this.data.list.concat(list)
          this.setData({
            list: this.data.list
          })
        } else {
          wx.showModal({
            title: '对不起',
            content: '',
            showCancel: false
          })
        }
        wx.stopPullDownRefresh()
      }).catch(err => {
        wx.showModal({
          title: '对不起',
          content: JSON.stringify(err) || '网络错误，请稍后再试',
          showCancel: false
        })
      })
    },
    onReachLower() {
      if (this.data.finished) {
        return
      }
      let list = this.data.list
      let total = this.data.total
      if (list.length >= total) {
        this.data.finished = true
        this.setData({
          finished: this.data.finished
        })
      } else {
        this.data.pageIndex += 1
        this.concatList()
      }
    },
    onLoad(options) {
      // this.onceQuery()
    },
    onReady() { },
    onShow() {
      app.memberReadyCb = () => {
        this.data.openid = app.globalData.fans.AppOpenID
        this.initDatas()
        // this.totalQuery()
      }
      app.fansReadyCb = () => {
        app.checkMember()
      }
      app.init()
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})